#include "ets_sys.h"
#include "os_type.h"
#include "osapi.h"
#include "mem.h"
#include "gpio.h"
#include "user_interface.h"

#include "driver/key.h"

#include"pwm.h"
#define PRIV_PARAM_START_SEC		0x3C
#define PRIV_PARAM_SAVE     0



/*Define the channel number of PWM*/
/*In this demo, we can set 3 for 3 PWM channels: RED, GREEN, BLUE*/
/*Or , we can choose 5 channels : RED,GREEN,BLUE,COLD-WHITE,WARM-WHITE*/
#define PWM_CHANNEL	5  //  5:5channel ; 3:3channel

#define LIGHT_RED       0
#define LIGHT_GREEN     1
#define LIGHT_BLUE      2
#define LIGHT_COLD_WHITE      3
#define LIGHT_WARM_WHITE      4


/*Definition of GPIO PIN params, for GPIO initialization*/
#define PWM_0_OUT_IO_MUX PERIPHS_IO_MUX_MTDI_U
#define PWM_0_OUT_IO_NUM 12
#define PWM_0_OUT_IO_FUNC  FUNC_GPIO12

#define PWM_1_OUT_IO_MUX PERIPHS_IO_MUX_MTDO_U
#define PWM_1_OUT_IO_NUM 15
#define PWM_1_OUT_IO_FUNC  FUNC_GPIO15

#define PWM_2_OUT_IO_MUX PERIPHS_IO_MUX_MTCK_U
#define PWM_2_OUT_IO_NUM 13
#define PWM_2_OUT_IO_FUNC  FUNC_GPIO13

#define PWM_3_OUT_IO_MUX PERIPHS_IO_MUX_MTMS_U
#define PWM_3_OUT_IO_NUM 14
#define PWM_3_OUT_IO_FUNC  FUNC_GPIO14

#define PWM_4_OUT_IO_MUX PERIPHS_IO_MUX_GPIO4_U
#define PWM_4_OUT_IO_NUM 4
#define PWM_4_OUT_IO_FUNC  FUNC_GPIO4

struct light_saved_param {
	uint32  pwm_period;
	uint32  pwm_duty[PWM_CHANNEL];
};

uint32 pwm_duty=0;


#if 1
uint32 io_info[][3] =  
{

	{PWM_0_OUT_IO_MUX,PWM_0_OUT_IO_FUNC,PWM_0_OUT_IO_NUM}, 
	{PWM_1_OUT_IO_MUX,PWM_1_OUT_IO_FUNC,PWM_1_OUT_IO_NUM},
	{PWM_2_OUT_IO_MUX,PWM_2_OUT_IO_FUNC,PWM_2_OUT_IO_NUM},
	{PWM_3_OUT_IO_MUX,PWM_3_OUT_IO_FUNC,PWM_3_OUT_IO_NUM},
	{PWM_4_OUT_IO_MUX,PWM_4_OUT_IO_FUNC,PWM_4_OUT_IO_NUM},
};
#else
uint32 io_info[][3] =
{	
	/*PWM1 FOR RED*/
        {PWM_1_OUT_IO_MUX,PWM_1_OUT_IO_FUNC,PWM_1_OUT_IO_NUM},
        /*PWM2 FOR GREEN*/
	{PWM_2_OUT_IO_MUX,PWM_2_OUT_IO_FUNC,PWM_2_OUT_IO_NUM},
        {PWM_3_OUT_IO_MUX,PWM_3_OUT_IO_FUNC,PWM_3_OUT_IO_NUM},
        /*PWM0 FOR BLUE*/
	{PWM_0_OUT_IO_MUX,PWM_0_OUT_IO_FUNC,PWM_0_OUT_IO_NUM},
        {PWM_4_OUT_IO_MUX,PWM_4_OUT_IO_FUNC,PWM_4_OUT_IO_NUM},
};

#endif

struct light_saved_param light_param;
//ICACHE_FLASH_ATTR

void ICACHE_FLASH_ATTR do_other_thing(unsigned char R, unsigned char G, unsigned char B, unsigned char W)
{
	light_param.pwm_period=1000;
	if (R == 255)
		light_param.pwm_duty[LIGHT_RED]= 22222;
	else
		light_param.pwm_duty[LIGHT_RED]= R * 86;

	if (G == 255)
		light_param.pwm_duty[LIGHT_GREEN]= 22222;
	else
		light_param.pwm_duty[LIGHT_GREEN]= G * 86;

	light_param.pwm_duty[LIGHT_BLUE]= 0;

	if (B == 255)
		light_param.pwm_duty[LIGHT_COLD_WHITE]= 22222;
	else
		light_param.pwm_duty[LIGHT_COLD_WHITE]= B * 86; // blue 

	if (W == 255)
		light_param.pwm_duty[LIGHT_WARM_WHITE]= 22222;
	else
		light_param.pwm_duty[LIGHT_WARM_WHITE]= W * 86;//W

	pwm_init(light_param.pwm_period,  light_param.pwm_duty ,5,io_info);
}


void  ICACHE_FLASH_ATTR pen_color(unsigned char R, unsigned char G, unsigned char B, unsigned char W)
{
    do_other_thing(R, G, B, W);
#if 0
	light_param.pwm_period=1000;
	if (R == 255)
		light_param.pwm_duty[LIGHT_RED]= 22222;
	else
		light_param.pwm_duty[LIGHT_RED]= R * 86;

	if (G == 255)
		light_param.pwm_duty[LIGHT_GREEN]= 22222;
	else
		light_param.pwm_duty[LIGHT_GREEN]= G * 86;

	light_param.pwm_duty[LIGHT_BLUE]= 0;

	if (B == 255)
		light_param.pwm_duty[LIGHT_COLD_WHITE]= 22222;
	else
		light_param.pwm_duty[LIGHT_COLD_WHITE]= B * 86; // blue 

	if (W == 255)
		light_param.pwm_duty[LIGHT_WARM_WHITE]= 22222;
	else
		light_param.pwm_duty[LIGHT_WARM_WHITE]= W * 86;//W

	pwm_init(light_param.pwm_period,  light_param.pwm_duty ,5,io_info);
#endif
}

void ICACHE_FLASH_ATTR pen_color2(unsigned int R, unsigned int G, unsigned int B, unsigned int W)
{
	light_param.pwm_period=1000;
	light_param.pwm_duty[LIGHT_RED]= R * 86;
	light_param.pwm_duty[LIGHT_GREEN]= G * 86;
	light_param.pwm_duty[LIGHT_BLUE]= 0;
	light_param.pwm_duty[LIGHT_COLD_WHITE]= B * 86; // blue 
	light_param.pwm_duty[LIGHT_WARM_WHITE]= W * 86;//W
	pwm_init(light_param.pwm_period,  light_param.pwm_duty ,5,io_info);
}
